﻿using FluentValidation.Attributes;
using Newtonsoft.Json;
using SDF.Core;
using SDF.Core.Infrastructure;
using SDF.Plugin.Misc.DynamicEntity.Domain;
using SDF.Plugin.Misc.DynamicEntity.Domain.Enums;
using SDF.Plugin.Misc.DynamicEntity.Validators.DynamicEntity;
using SDF.Services.Localization;
using SDF.Web.Framework;
using SDF.Web.Framework.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;

namespace SDF.Plugin.Misc.DynamicEntity.Models
{
    [Validator(typeof(DynamicEntityItemValidator))]
    public class DynamicEntityItemModel : BaseSDFEntityModel
    {
        private readonly ILocalizationService _localizationService = EngineContext.Current.Resolve<ILocalizationService>();
        private readonly IWorkContext _workContext = EngineContext.Current.Resolve<IWorkContext>();

        [SDFResourceDisplayName("Plugins.Misc.DynamicEntity.CodeName")]
        public string CodeName { get; set; }

        [SDFResourceDisplayName("Plugins.Misc.DynamicEntity.Name")]
        public string Name { get; set; }

        [SDFResourceDisplayName("Plugins.Misc.DynamicEntity.Delete")]
        public bool Delete { get; set; }

        [SDFResourceDisplayName("Plugins.Misc.DynamicEntity.CreateTimeOnUtc")]
        public DateTime CreateTimeOnUtc { get; set; }

        [JsonIgnore]
        [ScriptIgnore]
        public virtual DynamicEntityHeaderModel Header { get; set; }

        [SDFResourceDisplayName("Plugins.Misc.DynamicEntity.HeaderId")]
        public int HeaderId { get; set; }

        [SDFResourceDisplayName("Plugins.Misc.DynamicEntity.FieldType")]
        public EntityFieldTypeEnum FieldType { get; set; }

        [SDFResourceDisplayName("Plugins.Misc.DynamicEntity.FieldType")]
        public string FieldTypeName
        {
            get
            {
                return this.FieldType.GetLocalizedEnum(_localizationService, _workContext);
            }
        }

        [SDFResourceDisplayName("Plugins.Misc.DynamicEntity.FieldLength")]
        public int FieldLength { get; set; }

        [SDFResourceDisplayName("Plugins.Misc.DynamicEntity.DefaultValue")]
        public string DefaultValue { get; set; }

        [SDFResourceDisplayName("Plugins.Misc.DynamicEntity.ParamDirection")]
        public ParamDirectionEnum ParamDirection { get; set; }

        [SDFResourceDisplayName("Plugins.Misc.DynamicEntity.ParamDirection")]
        public string ParamDirectionName
        {
            get
            {
                return this.ParamDirection.GetLocalizedEnum(_localizationService, _workContext);
            }
        }

        [SDFResourceDisplayName("Plugins.Misc.DynamicEntity.IsStruct")]
        public bool IsStruct { get; set; }

        [SDFResourceDisplayName("Plugins.Misc.DynamicEntity.SortNo")]
        public int SortNo { get; set; }

        [ScriptIgnore]
        [JsonIgnore]
        public virtual DynamicEntityHeader ReferenceEntity { get; set; }
    }
}